using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace OA.UI
{
    public class LoginValidateAttribute:ActionFilterAttribute
    {


        public static ActionResult ValidLogin(System.Web.HttpContextBase context)
        {
            ActionResult action = null;
            if (BLL.LoginUser.IsLogin() == false)   //未登陆
            {
                var help=new UrlHelper(context.Request.RequestContext);
                var url = help.RouteUrl("Default", new { controller = "Home", action = "Login" });
                if (context.Request.IsAjaxRequest() == false)
                {
                    url = Utils.GetUrlParames(url, "BackUrl", context.Request.Url.AbsoluteUri);
                    action= new RedirectResult(url);
                }
                else
                {
                    Common.Result ret = new Common.Result(false, 0, "nologin");
                    ret.data = url;
                    action = new ContentResult() { Content = Newtonsoft.Json.JsonConvert.SerializeObject(ret) };
                }
            }
            return action;
        }


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Result = ValidLogin(filterContext.HttpContext);
            base.OnActionExecuting(filterContext);
        }
    }
}
